 use "$data_out/Discharges.dta", clear 

estimates clear




//need to be in sample at beginning of period (6 months, 1 year)
	foreach outcome in scripts_365 prov7  day_180  {

	
eststo `outcome'_1:	ivreghdfe `outcome' i.age_bin race_white female junior_enlisted college married longevity afqt_p (ed_prescription = propensity) , absorb(hym hd i.diag mos) cluster(provID) 
estadd ysumm, replace
	
	
eststo `outcome'_2:	ivreghdfe `outcome' i.age_bin race_white female junior_enlisted college married longevity afqt_p (ed_prescription = propensity) if discharge_180 == 0, absorb(hym hd i.diag mos) cluster(provID) 
estadd ysumm, replace

eststo `outcome'_3:	ivreghdfe `outcome' i.age_bin race_white female junior_enlisted college married longevity afqt_p (ed_prescription = propensity) if !inrange(discharge_date, encdate + 181, encdate+365), absorb(hym hd i.diag mos) cluster(provID) 
estadd ysumm, replace

}


merge 1:1 pid_pde encdate using "$data_out/promotions.dta", keep(3) nogen

foreach outcome in promotion1 {

eststo `outcome'_1:	ivreghdfe `outcome' i.age_bin race_white female i.grade college married longevity afqt_p (ed_prescription = propensity) , absorb(hym hd i.diag mos) cluster(provID) 
estadd ysumm, replace
	
	
eststo `outcome'_2:	ivreghdfe `outcome' i.age_bin race_white female i.grade college married longevity afqt_p (ed_prescription = propensity) if discharge_180 == 0, absorb(hym hd i.diag mos) cluster(provID) 
estadd ysumm, replace

eststo `outcome'_3:	ivreghdfe `outcome' i.age_bin race_white female i.grade college married longevity afqt_p (ed_prescription = propensity) if !inrange(discharge_date, encdate + 181, encdate+365), absorb(hym hd i.diag mos) cluster(provID) 
estadd ysumm, replace


}

use "$data_out/Discharges.dta", clear 

 merge 1:1 pid_pde encdate using "$data_out/flags.dta", keep(3) nogen
 
 foreach outcome in  adverse_flag {
eststo `outcome'_1:	ivreghdfe `outcome' i.age_bin race_white female junior_enlisted college married longevity afqt_p (ed_prescription = propensity), absorb(hym hd i.diag mos) cluster(provID) 
estadd ysumm, replace
	
	
eststo `outcome'_2:	ivreghdfe `outcome' i.age_bin race_white female junior_enlisted college married longevity afqt_p (ed_prescription = propensity) if discharge_180 == 0, absorb(hym hd i.diag mos) cluster(provID) 
estadd ysumm, replace

eststo `outcome'_3:	ivreghdfe `outcome' i.age_bin race_white female junior_enlisted college married longevity afqt_p (ed_prescription = propensity) if !inrange(discharge_date, encdate + 181, encdate+365), absorb(hym hd i.diag mos) cluster(provID) 
estadd ysumm, replace




}




//Tables
label var ed_prescription "Prescriptions in 1 Year"
	
	#delimit ;

esttab  scripts_365_1 scripts_365_2 scripts_365_3 using "Attrition.rtf" , 
	replace label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats( ,
	fmt(a2 %-9.0fc))
	nonotes
		keep(ed_prescription)
		mtitles("Main Results" "Excl Attrition < 6 months" "Excl Attrition 6-12 Months")
		title(Controlling for Intensity of Medical Treatment During ED Visit);
#delimit cr

label var ed_prescription "7 Providers in 1 Year"
	
	#delimit ;

esttab prov7_1 prov7_2 prov7_3  using "Attrition.rtf" , 
	append label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats( ,
	fmt(a2 %-9.0fc))
	nonotes
	nomtitles
	nonum
		keep(ed_prescription);
#delimit cr


label var ed_prescription "180 Days of Supply in 1 Year"
	
	#delimit ;

esttab day_180_1 day_180_2 day_180_3  using "Attrition.rtf" , 
	append label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats( ,
	fmt(a2 %-9.0fc))
	nonotes
	nomtitles
	nonum
		keep(ed_prescription);
#delimit cr

label var ed_prescription "Promotion in 1 Year"
	
	#delimit ;

esttab promotion1_1 promotion1_2 promotion1_3  using "Attrition.rtf" , 
	append label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats( ,
	fmt(a2 %-9.0fc))
	nonotes
	nomtitles
	nonum
		keep(ed_prescription);
#delimit cr

label var ed_prescription "Adverse Flag in 1 Year (Army Enlisted Only)"
	
	#delimit ;

esttab adverse_flag_1 adverse_flag_2 adverse_flag_3  using "Attrition.rtf" , 
	append label se star(* 0.10 ** 0.05 *** 0.01)
	b(4) se(4)
	stats( ,
	fmt(a2 %-9.0fc))
	nonotes
	nomtitles
	nonum
		keep(ed_prescription);
#delimit cr

//means

esttab scripts_365_1 scripts_365_2 scripts_365_3 using "attrition_means.rtf" , replace label se star(* 0.10 ** 0.05 *** 0.01) ///
	b(4) se(4) stats(ymean , fmt(%-9.4fc)  label("Prescriptions in 1 Year")) 		mtitles("Main Results" "Excl Attrition < 6 months" "Excl Attrition 6-12 Months")  ///
		keep(ed_prescription)
		
esttab prov7_1 prov7_2 prov7_3  using "attrition_means.rtf" , append label se star(* 0.10 ** 0.05 *** 0.01) ///
	b(4) se(4) stats(ymean , fmt( %-9.4fc)  label("7 Providers in 1 Year")) 		nomtitles nonotes nonum ///
		keep(ed_prescription)
		
		esttab day_180_1 day_180_2 day_180_3 using "attrition_means.rtf" , append label se star(* 0.10 ** 0.05 *** 0.01) ///
	b(4) se(4) stats(ymean , fmt( %-9.4fc)  label("180 Days of Supply in 1 Year")) 		nomtitles nonotes nonum ///
		keep(ed_prescription)

	esttab promotion1_1 promotion1_2 promotion1_3 using "attrition_means.rtf" , append label se star(* 0.10 ** 0.05 *** 0.01) ///
	b(4) se(4) stats(ymean , fmt(%-9.4fc)  label("Promotion in 1 Year")) 		nomtitles nonotes nonum ///
		keep(ed_prescription)
		
			esttab adverse_flag_1 adverse_flag_2 adverse_flag_3  using "attrition_means.rtf" , append label se star(* 0.10 ** 0.05 *** 0.01) ///
	b(4) se(4) stats(ymean , fmt( %-9.4fc)  label("Adverse Flag in 1 Year (Army Enlisted Only)")) 		nomtitles nonotes nonum ///
		keep(ed_prescription)

	
